﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RouteConfig.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   The route config.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog
{
    using System.Collections;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    using EcBlog.Api.MetaWeblog;
    using EcBlog.Data;
    using EcBlog.Migrations;
    using EcBlog.Models;
    using EcBlog.Routing;

    /// <summary>
    /// The route config.
    /// </summary>
    public class RouteConfig
    {
        /// <summary>
        /// The register routes.
        /// </summary>
        /// <param name="routes">
        /// The routes.
        /// </param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            ClearCache();

            routes.IgnoreRoute("{controller}/{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{controller}/{action}/{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("Content/{*pathInfo}");
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("elmah.axd");
            routes.IgnoreRoute("robots.txt");

            routes.MapRoute("sitemap", "sitemap.xml", new { controller = "Rss", action = "SiteMap" });
            routes.MapRoute("newssitemap", "sitemap-news.xml", new { controller = "Rss", action = "SiteMap" });
            routes.MapRoute("feed", "feed", new { controller = "Rss", action = "Feed" });

            var hasSetupCompleted = Configuration.HasSetupCompleted();
            if (!hasSetupCompleted)
            {
                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters 
                    new { controller = "install", action = "index", id = UrlParameter.Optional });

                return;
            }

            var unitOfWork = DependencyResolver.Current.GetService<IEcUnitOfWork>();
            var urlFormat = unitOfWork.SiteSettingRepository[SiteSettingKey.SlugUrlFormat];
            var urlSuffix = unitOfWork.SiteSettingRepository[SiteSettingKey.UrlSuffix];

            routes.Add(new DynamicPageRoute());
            routes.Add(new Route("{weblog}", null, new RouteValueDictionary(new { weblog = "blogapi" }), new MetaWeblogRouteHandler()));

            routes.MapRoute(
                "TagPosts",
                "category/{tagSlug}/{pageNo}" + urlSuffix,
                new { controller = "Blog", action = "TagPosts", pageNo = 1 });

            routes.MapRoute(
                "BlogPost1",
                "page/{pageNo}" + urlSuffix,
                new { controller = "Blog", action = "BlogPosts", pageNo = 1 });

            routes.MapRoute(
                "BlogPosts2",
                "archive/{year}/{month}/{pageNo}" + urlSuffix,
                new { controller = "Blog", action = "BlogPostArchives", pageNo = 1 });

            routes.MapRoute(
                "BlogSearch",
                "search/{searchKey}/{pageNo}" + urlSuffix,
                new { controller = "Blog", action = "Search", pageNo = 1 });

            routes.MapRoute(
                "BlogPost",
                "post/" + urlFormat + urlSuffix,
                new { controller = "Blog", action = "BlogPost" });

            routes.MapRoute(
               "DefaultSlug",
               "{pageSlug}" + urlSuffix,
               new { controller = "Blog", action = "DynamicPageSlug" });

            routes.MapRoute(
                "Default",
                "{pageNo}" + urlSuffix,
                new { controller = "Blog", action = "BlogPosts", pageNo = 1 });

            routes.MapRoute(
                "DefaultController",
                "{controller}/{action}/{id}",
                new { controller = "Widget", action = "TagList", id = UrlParameter.Optional });
            RouteHandler<RedirectHandler>.Assign(RouteTable.Routes);
            routes.RouteExistingFiles = true;
        }

        private static void ClearCache()
        {
            var removeList = (from DictionaryEntry cacheItem in HttpRuntime.Cache
                                     select cacheItem.Key.ToString()).ToList();
            foreach (var key in removeList)
            {
                HttpRuntime.Cache.Remove(key);
            }
        }
    }
}